package com.hnkywl.sts.service.taskdeal;

import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.zjfb.Kyzjzs;
import com.hnkywl.sts.service.sfgl.SkshService;
import com.hnkywl.sts.service.xsgl.XsdService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-5-12
 * Time: 下午5:00
 * To change this template use File | Settings | File Templates.
 * 资金分配到销售单
 */
@Service
@Transactional
public class DoCapitalAllocation {

    // 定义日志
    private static Log log = LogFactory.getLog(DoCapitalAllocation.class);
    // 付款service
    private SkshService skshService;

    // 销售单service
    private XsdService xsdService;


    //  根据内部公司和企业机构进行分组求和(某段时间之前的)
    public void setCapitalAllocation() {
        log.info("资金分配开始执行setCapitalAllocation.............................");
        try {
            Date date = new Date();
            // 根据日期，查询需要统计的付款金额
            List kyzjList = this.skshService.findKyzjByDate(date);
            if (null != kyzjList && kyzjList.size() > 0) {
                // 累计付款总金额
                for (int i = 0; i < kyzjList.size(); i++) {
                    // 操作可用资金总数,已经存在则更新，否则保存
                    Object[] obj = (Object[]) kyzjList.get(i);
                    this.skshService.saveOrUpdate(Double.valueOf(obj[0].toString()), Long.valueOf(obj[1].toString()), Long.valueOf(obj[2].toString()));
                }
            }
            // 根据内部公司，企业机构，进行资金分配到对应的销售单
            doCaptitalAlloca();
            log.info("资金分配执行成功setCapitalAllocation.............................");
        } catch (Exception e) {
            log.info("setCapitalAllocation 错误：" + e);
        }
    }

    // 进行资金分配到销售单
    private void doCaptitalAlloca() {
        // 查询累计可用的付款金额
        List<Kyzjzs> kyzjzsList = this.skshService.findAllKyzjzs();
        if (null != kyzjzsList && kyzjzsList.size() > 0) {
            for (Kyzjzs kyzjzs : kyzjzsList) {
                // 可用资金总数
                Double kyzjsum = kyzjzs.getKyzjzs();
                // 查询销售单
                List<Xsd> xsdList = this.xsdService.findAllXsdBySfyfk(kyzjzs.getNbgs().getId(), kyzjzs.getQyjg().getId());
                if (null != xsdList && xsdList.size() > 0) {
                    doDealZjfp(kyzjzs, xsdList, kyzjsum);
                }
            }
        }
    }

    // 处理资金分配
    private void doDealZjfp(Kyzjzs kyzjzs, List<Xsd> xsdList, Double kyzjsum) {
        for (Xsd xsd : xsdList) {
            if (null != xsd.getXsje() && null != xsd.getId()) {
                // 查询该销售单，已经付过款值
                Double xsdYfk = this.xsdService.getAllYfk(xsd.getId());
                // 剩余待付款金额
                Double sydfje = xsd.getXsje() - xsdYfk;
                // 进行付款
                kyzjsum = kyzjsum - sydfje;
                if (kyzjsum >= 0) {
                    // 更新销售单为已完成支付, 插入分配记录表
                    this.xsdService.saveInfo(xsd.getId(), "1", sydfje);
                    // 更新剩余资金总数
                    this.skshService.updateKyzjzs(kyzjzs.getId(), kyzjsum);
                } else {
                    // 更新销售单为未完成支付, 插入分配记录表
                    this.xsdService.saveInfo(xsd.getId(), "0", kyzjsum + xsd.getXsje());
                    // 更新剩余资金总数
                    this.skshService.updateKyzjzs(kyzjzs.getId(), 0d);

                    break;
                }
            }
        }
    }


    @Autowired
    public void setSkshService(SkshService skshService) {
        this.skshService = skshService;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }
}
